<?php


namespace tools\lib\main\model;

use tools\lib\main\Common;

/**
 * 创建字典
 * Class Dictionary
 * @package app\common\command\create
 */
class Dictionary extends Common
{
    public function template()
    {

        $template = <<<EOT
<?php

{$this->getFileNamespace()}

class {$this->className()}
{
    public \$word = {$this->getFields()};
}
EOT;
        return $template;
    }

    public function getFields()
    {
        $columns = tableColumns($this->parameter->getName());

        $concat = function ($filed, $description, $intro = '') {
            return ($intro ? ('        //' . $intro . "\n") : '')
                . '        \''
                . $filed
                . '\''
                . ' => '
                . '\''
                . $description
                . '\','
                . "\n";
        };

        $fields = '';
        if ($columns) {
            foreach ($columns as $column) {
                if (in_array($column['Field'], [
                    'id',
                    'create_time',
                    'delete_time',
                    'update_time',
                ])) {
                    continue;
                }

                $comment = $column['Comment'];

                $comment = explode(' ', $comment);

                $description = isset($comment[0]) ? $comment[0] : '';

                if (count($comment) > 1) {
                    $intro = implode(' ', array_slice($comment, 1));
                } else {
                    $intro = '';
                }

                $fields .= $concat($column['Field'], $description, $intro);

                // 设置 status_text
                if (isSatisfyConstant($column)) {
                    $fields .= $concat($column['Field'] . '_text', $description);
                }
            }
        }

        $fields = '['
            . "\n"
            . $fields
            . '    ]';

        return $fields;
    }
}